<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目" prop="itemId">
        <el-select v-model="queryParams.itemId" placeholder="请选择项目" clearable style="width: 200px;">
          <el-option v-for="(label, value) in itemIdMap" :key="value" :label="label" :value="value" />
        </el-select>
      </el-form-item>

      <el-form-item label="套餐" prop="packageId">
        <el-select v-model="queryParams.packageId" placeholder="请选择套餐" clearable style="width: 200px;">
          <el-option v-for="(label, value) in packageIdMap" :key="value" :label="label" :value="value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['health:itemPackage:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['health:itemPackage:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['health:itemPackage:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['health:itemPackage:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemPackageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="套餐项目ID" align="center" prop="bizpackagePhysicalItemId" />
      <el-table-column label="项目" align="center">
        <template #default="scope">
          {{ itemIdMap[scope.row.itemId] || '未知' }}
        </template>
      </el-table-column>

      <el-table-column label="套餐" align="center">
        <template #default="scope">
          {{ packageIdMap[scope.row.packageId] || '未知' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['health:itemPackage:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['health:itemPackage:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改套餐项目关联对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="itemPackageRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目 ID" prop="itemId">
          <el-select v-model="form.itemId" placeholder="请选择项目" style="width: 200px;">
            <el-option v-for="(label, value) in itemIdMap" :key="value" :label="label" :value="value" />
          </el-select>
        </el-form-item>

        <el-form-item label="套餐 ID" prop="packageId">
          <el-select v-model="form.packageId" placeholder="请选择套餐" style="width: 200px;">
            <el-option v-for="(label, value) in packageIdMap" :key="value" :label="label" :value="value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ItemPackage">
import { listItemPackage, getItemPackage, delItemPackage, addItemPackage, updateItemPackage } from "@/api/health/itemPackage"
import { listPackage } from "@/api/health/package"; // 确保导入了套餐的API
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/health/item"



const { proxy } = getCurrentInstance()

const itemPackageList = ref([])
const packageList = ref([]);
const itemList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    itemId: null,
    packageId: null
  },
  itemIds: {
     pageNum: 1,
    pageSize: 100,
    itemId: null,
    itemName: null,
    itemStaue: null,
    price: null,
    normalRange: null,
    remark: null
  },
  rules: {
  }
})
// const packageIdMap1 = {
//   1: '入职体检套餐',
//   2: '女性专项体检套餐',
//   3: '中老年体检套餐',
//   4: '全面深度体检套餐',
//   5: '儿童成长体检套餐'
// };
const packageIdMap = computed(() => {
  return packageList.value.reduce((acc, item) => {
    acc[item.packageId] = item.packageName;
    return acc;
  }, {});
});


// const itemIdMap1= {
//   1: '血常规',
//   2: '尿常规',
//   3: '肝功能全套',
//   4: '肾功能三项',
//   5: '血脂四项',
//   6: '心电图',
//   7: '胸部X光',
//   8: '腹部B超',
//   9: '甲状腺功能三项',
//   10: '癌胚抗原(CEA)'
// };
const itemIdMap = computed(() => {
  return itemList.value.reduce((acc, item) => {
    acc[item.itemId] = item.itemName;
    return acc;
  }, {});
});

const { queryParams, itemIds,form, rules } = toRefs(data)

/** 查询套餐项目关联列表 */
function getList() {
  loading.value = true
  listItemPackage(queryParams.value).then(response => {
    itemPackageList.value = response.rows
    total.value = response.total
    loading.value = false
  });
  listPackage().then(response => { // 加载套餐数据
    packageList.value = response.rows;
    total.value = response.total
    loading.value = false
  });
  listItem(itemIds.value).then(response => {
    itemList.value = response.rows
    console.log(response.rows);
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    bizpackagePhysicalItemId: null,
    itemId: null,
    packageId: null
  }
  proxy.resetForm("itemPackageRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.bizpackagePhysicalItemId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加套餐项目关联"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _bizpackagePhysicalItemId = row.bizpackagePhysicalItemId || ids.value
  getItemPackage(_bizpackagePhysicalItemId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改套餐项目关联"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["itemPackageRef"].validate(valid => {
    if (valid) {
      if (form.value.bizpackagePhysicalItemId != null) {
        updateItemPackage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addItemPackage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _bizpackagePhysicalItemIds = row.bizpackagePhysicalItemId || ids.value
  proxy.$modal.confirm('是否确认删除套餐项目关联编号为"' + _bizpackagePhysicalItemIds + '"的数据项？').then(function () {
    return delItemPackage(_bizpackagePhysicalItemIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('health/itemPackage/export', {
    ...queryParams.value
  }, `itemPackage_${new Date().getTime()}.xlsx`)
}

getList()
</script>
